# 原码 反码  补码
def cal(num):
    if num >= 128 or num <= -128:
        print("====越界=====")
        return
    if num >= 0:
        str = "0"
        for index in range(8 - len(bin(num)[2:]) - 1):
            str += "0"
        str += bin(num)[2:]
        print("补码:" + str)
        print("反码:" + str)
        print("原码:" + str)
        return num
    bin_num = bin(num)[3:]
    str = "1"
    for index in range(8 - len(bin(num)[3:]) - 1):
        str += "0"
    # bin_num +=str
    str += bin_num
    print("原码:" + str)
    arr1 = []
    for index in range(len(str)):
        arr1.append(int(str[index:index + 1]))
    arr2 = [] + arr1
    for num in range(len(arr1)):
        if num != 0:
            if arr1[num] == 1:
                arr2[num] = 0
            elif arr1[num] == 0:
                arr2[num] = 1

    print("反码：" + arr2.__str__().replace(',', '')[1:-1])
    arr3 = [] + arr2
    help(arr2, arr3, len(arr3) - 1)
    bm = arr3.__str__().replace(',', '')[1:-1]
    print("补码：" + bm)


# 递归函数体
def help(arr1, arr2, index):
    if index == 0:
        return
    if arr1[index] == 1:
        arr2[index] = 0
        help(arr1, arr2, index - 1)
    elif arr1[index] == 0:
        arr2[index] = arr1[index] + 1


cal(int(input("请输入你要测试的数字:\n")))
